﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core.DAL.SQL;
using Medianamik.Core.Reflection;

namespace Medianamik.Core.Querying
{
    public static class IMemberInfoExtensions
    {
        public static Column GetColumn(this IMemberInfo pMember, ITable pTable)
        {
            var attrs = pMember.MemberInfo.GetCustomAttributes(typeof(ColumnMapping), true);
            if (attrs.Length == 0)
            {
                throw new NotSupportedException("Missing ColumnMapping attribute on : " + pMember.MemberInfo.Name);
            }
            var colName = ((ColumnMapping)attrs[0]).ColumnName;
            if (!pTable.Contains(colName))
            {
                throw new NotSupportedException("Column '" + colName + "'  Not Found in Table representation of '" + pTable.Name + "'");
            }
            return pTable[colName];
        }

    }
}
